//! 五.控制器与路由（第一步在Entity）

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using TM.Api.Entity;
using TM.Api.Params;
using TM.Api.Repository;
namespace TM.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {

        //? IConfiguration 是用来加载配置值的，可以加载内存键值对、JSON或XML配置文件，通常用来加载缺省的appsettings.json。
        private IConfiguration _Configuration;
        private IRepository<Users> _Repository;


        /// <summary>
        ///UsersController的构造函数
        /// </summary>
        /// <param name="configuration">加载配置</param>
        /// <param name="repository">实现接口</param>
        public UsersController(IConfiguration configuration, IRepository<Users> repository)
        {
            _Configuration = configuration;
            _Repository = repository;
        }


        [HttpGet]
        /// <summary>
        /// 查找所有user。
        /// </summary>
        /// <returns></returns>
        public dynamic Get()
        {
            var users = _Repository.Table.ToList();
            return new
            {
                Code = 1000,
                Data = users,
                Msg = "搜索成功"
            };
        }

        [HttpGet("{id}")]
        /// <summary>
        /// 查找指定user。
        /// </summary>
        /// <returns></returns>
        public dynamic Get(int id)
        {
            var user = _Repository.GetById(id);
            return new
            {
                Code = 1001,
                Data = user,
                Msg = "搜索成功"
            };
        }


        [HttpPost]
        /// <summary>
        /// 添加user。
        /// </summary>
        /// <returns></returns>
        public dynamic Post(CreateUser createUser)
        {
            var username = createUser.Username.Trim();
            var password = createUser.Password.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "账号或密码不能为空"
                };
            }
            var user = new Users
            {
                Username = createUser.Username.Trim(),
                Password = createUser.Password.Trim()
            };
            _Repository.Insert(user);
            return new
            {
                Code = 1000,
                Data = user,
                Msg = "创建成功"
            };
        }

        [HttpPut("{id}")]
        /// <summary>
        /// 更改指定user。
        /// </summary>
        /// <returns></returns>
        public dynamic Put(int id, CreateUser createUser)
        {
            var username = createUser.Username.Trim();
            var password = createUser.Password.Trim();
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "账号或密码不能为空"
                };
            }
            var user = _Repository.GetById(id);
            if (user == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "用户不存在"
                };
            }
            user.Username = createUser.Username.Trim();
            user.Password = createUser.Password.Trim();
            _Repository.Update(user);
            return new
            {
                Code = 1000,
                Data = user,
                Msg = "修改成功"
            };
        }
        [HttpDelete("{id}")]
        /// <summary>
        /// 删除指定user。
        /// </summary>
        /// <returns></returns>
        public dynamic Delete(int id)
        {
            _Repository.Delete(id);
            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };
        }
    }
}

//! 写完路由就可以运行咯~ 试试吧
